<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>图书管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container {
            margin-top: 30px;
        }
        .table th {
            background-color: #f8f9fa;
        }
        .btn-group {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2 class="text-center mb-4">图书管理系统</h2>
        
        <div class="btn-group">
            <button type="button" class="btn btn-primary" onclick="location.href='add'">添加图书</button>
        </div>

        <table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>书名</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>ISBN</th>
                    <th>价格</th>
                    <th>库存</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${books}" var="book">
                    <tr>
                        <td>${book.id}</td>
                        <td>${book.name}</td>
                        <td>${book.author}</td>
                        <td>${book.publisher}</td>
                        <td>${book.isbn}</td>
                        <td>￥${book.price}</td>
                        <td>${book.stock}</td>
                        <td>
                            <button class="btn btn-sm btn-info" onclick="location.href='edit?id=${book.id}'">编辑</button>
                            <button class="btn btn-sm btn-danger" onclick="deleteBook(${book.id})">删除</button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/js/bootstrap.bundle.min.js"></script>
    <script>
        function deleteBook(id) {
            if (confirm('确定要删除这本书吗？')) {
                $.ajax({
                    url: '${pageContext.request.contextPath}/api/books/' + id,
                    type: 'DELETE',
                    success: function(result) {
                        if (result) {
                            alert('删除成功！');
                            location.reload();
                        } else {
                            alert('删除失败！');
                        }
                    },
                    error: function() {
                        alert('删除失败！');
                    }
                });
            }
        }
    </script>
</body>
</html> 